package router

import (
	adminApi "TaskAssignmentSystem/internal/admin/api"
	"TaskAssignmentSystem/internal/admin/pkg"
	"TaskAssignmentSystem/internal/pkg/middleware/handler"
	"TaskAssignmentSystem/pkg/utils"
	"github.com/go-playground/validator"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/context"
	"github.com/kataras/iris/v12/versioning"
)

var App struct {
	Application  *iris.Application
	IsVerifySign bool `json:"is_verify_sign"`
}

func init() {
	App.Application = iris.Default()
	App.Application.Logger().SetLevel("info")
	App.IsVerifySign = utils.VerifySign
	App.Application.Validator = validator.New()
	handler.CustomErrorResponseHandler(App.Application)
	//App.Application.Get("/swagger/*any", swagger.WrapHandler(swaggerFiles.Handler))
	App.Application.HandleDir("/api/static", "./internal/static")
	//lmt := tollbooth.NewLimiter(3, &limiter.ExpirableOptions{DefaultExpirationTTL: 10 * time.Second})
	//marshal, _ := jsoniter.Marshal(iris.Map{"code": http.StatusNotAcceptable, "message": "请求太过频繁"})
	//lmt.SetMessage(string(marshal))
	//lmt.SetMessageContentType("application/json")
	//lmt.SetOverrideDefaultResponseWriter(true)
	//lmt.SetStatusCode(200)
	//lmt.SetIPLookups([]string{"RemoteAddr", "X-Forwarded-For", "X-Real-IP"})
	//// Set a custom content-type.
	//lmt.SetMessageContentType("application/json; charset=utf-8")
	//// Set a custom expiration TTL for token bucket.
	//lmt.SetTokenBucketExpirationTTL(time.Hour)
	//
	//// Set a custom expiration TTL for basic auth users.
	//lmt.SetBasicAuthExpirationTTL(time.Hour)
	//
	//// Set a custom expiration TTL for header entries.
	//lmt.SetHeaderEntryExpirationTTL(time.Hour)
	api := App.Application.Party("/api")
	//api.HandleDir("/static", "./internal/static")
	/*
	* 后台系统验证码
	 */
	//api.Get("/admin/captcha_pro", apiService.GenerateCaptchaHandler)

	apiVersion := versioning.NewGroup(api, "1.0.0")

	App.Application.Use(iris.Compression)
	if App.IsVerifySign {
		apiVersion.Party("/", pkg.VerifySign)
	}

	apiVersion.PartyFunc("/", RegisterApiV10Router)
	var AuthApiRouter iris.Party
	versioning.NotFoundHandler = func(ctx *context.Context) {
		ctx.JSON(iris.Map{"code": 1406, "message": versioning.DefaultDeprecationOptions.WarnMessage})
		return
	}
	AuthApiRouter = apiVersion.Party("/", adminApi.AuthenticatedHandler)
	AuthApiRouter.PartyFunc("/", RegisterApiV10AuthRouter)
}
